﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EstimationStudio.Core;

namespace EstimationStudio.UI
{
    public partial class AdjustmentVariabilityControl : UserControl
    {
        public AdjustmentVariabilityControl()
        {
            InitializeComponent();
        }

        public event EventHandler Changed;

        private void Option_CheckedChanged(object sender, EventArgs e)
        {
            if (Changed != null)
                Changed(this, EventArgs.Empty);
        }

        public Rating GetCurrentRating()
        {
            if (optVeryLow.Checked)
                return Rating.VeryLow;

            if (optLow.Checked)
                return Rating.Low;

            if (optHigh.Checked)
                return Rating.High;

            if (optVeryHigh.Checked)
                return Rating.VeryHigh;

            if (optExtremelyHigh.Checked)
                return Rating.ExtraHigh;

            return Rating.Nominal;
        }

        public AdjustmentVariability GetAdjustmentVariability()
        {
            Double veryLowValue, lowValue, highValue, veryHighValue, extremelyHighValue;

            Double.TryParse(lblVeryLow.Text, out veryLowValue);
            Double.TryParse(lblLow.Text, out lowValue);
            Double.TryParse(lblHigh.Text, out highValue);
            Double.TryParse(lblVeryHigh.Text, out veryHighValue);
            Double.TryParse(lblExtremelyHigh.Text, out extremelyHighValue);

            return new AdjustmentVariability(veryLowValue, lowValue, highValue, veryHighValue, extremelyHighValue);
        }

        public void SetAdjustmentVariability(AdjustmentVariability adjustmentVariability, Rating currentRating)
        {
            if (adjustmentVariability == null)
                return;

            lblVeryLow.Text = adjustmentVariability[Rating.VeryLow].ToString("N2");
            lblLow.Text = adjustmentVariability[Rating.Low].ToString("N2");
            lblNominal.Text = adjustmentVariability[Rating.Nominal].ToString("N2");
            lblHigh.Text = adjustmentVariability[Rating.High].ToString("N2");
            lblVeryHigh.Text = adjustmentVariability[Rating.VeryHigh].ToString("N2");
            lblExtremelyHigh.Text = adjustmentVariability[Rating.ExtraHigh].ToString("N2");

            switch (currentRating)
            {
                case Rating.VeryLow:
                    optVeryLow.Checked = true;
                    break;
                case Rating.Low:
                    optLow.Checked = true;
                    break;
                case Rating.Nominal:
                    optNominal.Checked = true;
                    break;
                case Rating.High:
                    optHigh.Checked = true;
                    break;
                case Rating.VeryHigh:
                    optVeryHigh.Checked = true;
                    break;
                case Rating.ExtraHigh:
                    optExtremelyHigh.Checked = true;
                    break;
                default:
                    break;
            }
        }
    }
}
